import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';

class BaseWidgets {
  static Widget divider() {
    return const Divider(
      color: Color(0xFFE7E7E7),
      height: 0.5,
      thickness: 0.5,
    );
  }

  static Widget dividerIndent({double indent = 15}) {
    return Divider(
      indent: indent,
      endIndent: indent,
      color: const Color(0xFFE7E7E7),
      height: 0.5,
      thickness: 0.5,
    );
  }

  static Widget dividerHIndent(double indent) {
    return Divider(
      indent: indent,
      endIndent: indent,
      color: const Color(0xFFE7E7E7),
      height: 1,
      thickness: 1,
    );
  }

  static Widget dividerV(double indent) {
    return VerticalDivider(
      indent: indent,
      endIndent: indent,
      color: const Color(0xFFE7E7E7),
      width: 1,
      thickness: 1,
    );
  }

  static void showInputDialog(
    BuildContext context,
    void Function(String text) onConfirm, {
    String title = "提示",
    String hint = "请输入区域名称",
  }) {
    final TextEditingController textController = TextEditingController();
    RxString text = ''.obs;
    showDialog(
      context: context,
      builder: (cxt) {
        return Center(
          child: SizedBox(
            width: 280,
            height: 180,
            child: Card(
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      color: Colors.white,
                      padding: const EdgeInsets.all(10),
                      child: Text(
                        title,
                        textAlign: TextAlign.center,
                        style: const TextStyle(
                          color: Colors.black87,
                          fontSize: 18,
                          fontWeight: FontWeight.normal,
                          decoration: TextDecoration.none,
                        ),
                      ),
                    ),
                    BaseWidgets.divider(),
                    Padding(
                      padding: const EdgeInsets.all(15),
                      child: TextField(
                        keyboardType: TextInputType.text,
                        controller: textController,
                        style: const TextStyle(
                          color: Color(0xE6000000),
                          fontSize: 14,
                        ),
                        textAlignVertical: TextAlignVertical.center,
                        decoration: InputDecoration(
                          hintText: hint,
                          contentPadding: const EdgeInsets.only(left: 10, right: 10),
                          enabledBorder: const OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(5)),
                            borderSide: BorderSide(
                              color: Colors.black12,
                              width: 1.0,
                            ),
                          ),
                          focusedBorder: const OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(5)),
                            borderSide: BorderSide(
                              color: Colors.black12,
                              width: 1.0,
                            ),
                          ),
                          hintStyle: const TextStyle(
                            color: Colors.black54,
                            fontSize: 14,
                          ),
                          //使用 GestureDetector 实现手势识别
                          suffixIcon: GestureDetector(
                            child: Obx(() => Offstage(
                              offstage: text.value == '',
                              child: const Icon(Icons.clear),
                            )),
                            //点击清除文本框内容
                            onTap: () {
                              text.value = '';
                              textController.clear();
                            },
                          ),
                        ),
                        //使用 onChanged 完成双向绑定
                        onChanged: (value) {
                          text.value = value;
                        },
                      ),
                    ),
                    BaseWidgets.divider(),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Expanded(
                          child: InkWell(
                            onTap: () => Get.back(),
                            child: const Padding(
                              padding: EdgeInsets.only(top: 10, bottom: 10),
                              child: Text(
                                "取消",
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                  color: Colors.black54,
                                  fontSize: 16,
                                  fontWeight: FontWeight.normal,
                                  decoration: TextDecoration.none,
                                ),
                              ),
                            ),
                          ),
                        ),
                        SizedBox(
                          height: 20,
                          child: BaseWidgets.dividerV(0),
                        ),
                        Expanded(
                          child: InkWell(
                            onTap: () {
                              if(textController.text.isEmpty) {
                                Fluttertoast.showToast(msg: hint);
                                return;
                              }
                              Get.back();
                              onConfirm(textController.text);
                            },
                            child: const Text(
                              "确定",
                              textAlign: TextAlign.center,
                              style: TextStyle(
                                color: Color(0xFF00A0E9),
                                fontSize: 16,
                                fontWeight: FontWeight.normal,
                                decoration: TextDecoration.none,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}